<template>
<div>
  <div class="t1">
    <span>个人订单</span>
  </div>
  <div style="padding: 10px 0">
    <el-input style="width: 200px" placeholder="请输入订单编号" suffix-icon="el-icon-search" v-model="num"></el-input>
    <el-button class="ml-5" type="primary" @click="load" plain>搜索</el-button>
    <el-button class="ml-5" type="warning" @click="reset" plain>重置</el-button>
  </div>

  <div style="margin: 10px 0">
    <el-button type="danger" @click="deleteBatch" plain>批量删除 <i class="el-icon-delete"></i></el-button>
  </div>

  <el-table :data="tableData" border stripe header-cell-class-name="headerBg"
            @selection-change="handleSelectionChange">
    <el-table-column type="selection" width="55" :selectable='checkboxInit'></el-table-column>
    <el-table-column type="index"
                     :index="this.indexStartNum"
                     label="序号"
                     width="100">
    </el-table-column>
    <el-table-column prop="id" label="ID" width="80" v-if="false"></el-table-column>
    <el-table-column prop="user.nickname" label="用户"></el-table-column>
    <el-table-column prop="num" label="编号" width="120"></el-table-column>
    <el-table-column prop="hospital.name" label="医院"></el-table-column>
    <el-table-column prop="department.name" label="科室"></el-table-column>
    <el-table-column prop="physician.nickname" label="医师" ></el-table-column>
    <el-table-column prop="stage.date" label="就诊日期"></el-table-column>
    <el-table-column prop="stage.timePeriod" label="就诊时段"></el-table-column>
<!--    <el-table-column prop="date" label="下单日期"></el-table-column>-->
    <el-table-column prop="state" label="挂号状态">
      <template slot-scope="scope">
        <div :style="{'color':scope.row.state === tag?'red':'#333'}" v-if="scope.row.state !== '已诊断'">{{scope.row.state}}</div>
        <div style="color: green" v-else>{{scope.row.state}}</div>
      </template>
    </el-table-column>
    <el-table-column label="操作" width="230px">
      <template slot-scope="scope">
        <el-button type="success" plain @click="comment(scope.row)" v-if="scope.row.state === '已诊断'">评价医师 <i class="el-icon-edit"></i></el-button>
        <el-button type="primary" plain @click="cancel(scope.row.id)" v-if="scope.row.state === '已挂号'">取消订单 <i class="el-icon-remove-outline"></i></el-button>
        <el-button type="danger" @click="del(scope.row.id)" plain v-if="scope.row.state !== '已挂号'">删除订单 <i class="el-icon-delete"></i></el-button>
      </template>
    </el-table-column>
  </el-table>
  <div style="padding: 10px 0">
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="pageNum"
      :page-sizes="[2, 5, 10, 20]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
  </div>

  <el-dialog title="评论信息" :visible.sync="dialogFormVisible" width="30%">
    <el-form label-width="80px" size="small">
      <el-form-item label="医生">
        <el-input type="text" v-model="physician.nickname" clearable :disabled="true"></el-input>
      </el-form-item>
      <el-input v-model="form.phyId" v-if="false"/>
      <input v-model="form.userId" v-if="false">
      <!--        <el-select clearable v-model="form.userId" placeholder="请选择">
                <el-option v-for="item in users" :key="item.id" :label="item.nickname" :value="item.id"></el-option>
              </el-select>-->
      <el-form-item label="评论">
        <el-input type="textarea" v-model="form.content" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="评分">
        <el-select v-model="form.score" placeholder="请选择评分">
          <el-option label="10" value="10"></el-option>
          <el-option label="9" value="9"></el-option>
          <el-option label="8" value="8"></el-option>
          <el-option label="7" value="7"></el-option>
          <el-option label="6" value="6"></el-option>
          <el-option label="5" value="5"></el-option>
          <el-option label="4" value="4"></el-option>
          <el-option label="3" value="3"></el-option>
          <el-option label="2" value="2"></el-option>
          <el-option label="1" value="1"></el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="dialogFormVisible = false">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </div>
  </el-dialog>

</div>
</template>

<script>
export default {
  name: "Order",
  data() {
    return {
      indexStartNum: 1,
      tableData: [],
      total: 0,
      pageNum: 1,
      pageSize: 10,
      num: "",
      user: localStorage.getItem("user") ? JSON.parse(localStorage.getItem("user")) : {},
      users: {},
      physician: [],
      hospital: [],
      departments: {},
      dialogFormVisible: false,
      form: {},
      tag: "已取消",
      multipleSelection: [],
      headerBg: 'headerBg'
    }
  },
  created() {
    //请求分页查询数据
    this.load()
  },
  methods: {
    load() {
      this.request.get("/order/personOrder", {
        params: {
          pageNum: this.pageNum,
          pageSize: this.pageSize,
          num: this.num,
          id: this.user.id
        }
      }).then(res => {
        console.log(res)
        this.tableData = res.data
        this.total = res.total
      })
    },
    checkboxInit(row,index){
      if(row.state === '已挂号'){
        return 0
      }else{
        return 1
      }
    },
    comment(row){
      this.physician = row.physician
      this.user = row.user
      this.form.phyId = row.phyId
      this.form.userId = row.userId
      this.dialogFormVisible = true
    },
    save() {
      this.form.userId = this.user.id
      this.request.post("/appraise/edit", this.form).then(res => {
        if (res) {
          this.$message.success("保存成功")
          this.dialogFormVisible = false
          this.load()
        } else {
          this.$message.error("保存失败")
        }
      })
    },
    cancel(id){
      this.$confirm('确定取消该订单吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        console.log(id)
        this.request.post("/order/updateStage?id=" + id).then(res => {
          if (res) {
            this.$message.success("取消成功")
            this.load()
          } else {
            this.$message.error("取消失败")
          }
        })
      });

    },
    del(id) {
      this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.request.delete("/order/del?id=" + id).then(res => {
          if (res) {
            this.$message.success("删除成功")
            this.load()
          } else {
            this.$message.error("删除失败")
          }
        })
      });
    },
    handleSelectionChange(val) {
      console.log(val)
      if (val.state === '已挂号'){
        this.$message.error("请取消订单后再删除！")
      }else{
        this.multipleSelection = val
      }
    },
    deleteBatch() {
      let ids = this.multipleSelection.map(v => v.id) //将对象数组变成纯id数组 [{},{},{}] => [1,2,3]
      this.$confirm('此操作将永久批量删除选中数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.request.delete("/order/delBatch", {data: ids}).then(res => {
          if (res) {
            this.$message.success("批量删除成功")
            this.load()
          } else {
            this.$message.error("批量删除失败")
          }
        })
      });
    },
    reset() {
      this.num = ""
      this.load()
    },
    handleSizeChange(pageSize) {
      console.log(pageSize)
      this.pageSize = pageSize
      this.load()
    },
    handleCurrentChange(pageNum) {
      console.log(pageNum)
      this.pageNum = pageNum
      this.load()
    },
  }
}
</script>

<style scoped>

</style>
